﻿#include "./AppComponent.hpp"
#include "oatpp/network/Server.hpp"
#include "controllers/index.hpp"
#include "utility/index.hpp"
#include "entity/index.hpp"
#include "components/index.hpp"
#include "components2/index.hpp"

void run() {
    /* Register Components in scope of run() method */
    AppComponent components;

    // 为 HTTP 请求创建路由器
    OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);

    /* Create MyController and add all of its endpoints to router */
    router->addController(std::make_shared<DefaultController>());
    router->addController(std::make_shared<UploadImageController>());
    router->addController(std::make_shared<UploadImagesController>());
    router->addController(std::make_shared<UploadFileController>());
    router->addController(std::make_shared<ImageGifController>());
    router->addController(std::make_shared<EntityController>());

    // 创建 HTTP 连接处理程序tatus

    OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler);

    // 创建 TCP 连接提供者
    OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);

    // 创建服务器，它接受提供的 TCP 连接并将其传递给 HTTP 连接处理程序
    oatpp::network::Server server(connectionProvider, connectionHandler);

    // 打印服务器端口
    OATPP_LOGI("ApiService", "Server running on port %s", connectionProvider->getProperty("port").getData());

    // 运行服务器
    server.run();
}

int main() {
    // 初始化 oatpp 环境
    oatpp::base::Environment::init();

    // 初始化设置
    utility::AppSettings::LoadConfig();
    entity::InitEntityTypes();
    components::InitComponentInstances();
    components2::InitComponentInstances();
    components::PageStatistics::InitPageStatistics();

    // 运行应用
    run();

    // 销毁 oatpp 环境
    oatpp::base::Environment::destroy();

    return 0;
}
